package xdu.lz.stage2_design_pattern.chapter17_worker_thread_pattern;


import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 模拟搬货到传送带的人
 */
public class TransportThread extends Thread {

    private final Channel channel;

    private static final Random random  = new Random(System.currentTimeMillis());

    private static AtomicInteger counter = new AtomicInteger(0);

    public TransportThread(String name,Channel channel) {
        super(name);
        this.channel = channel;
    }


    @Override
    public void run() {
        //一直在放货物到传送带
        while (true){
            Request request = new Request(Thread.currentThread().getName(), counter.getAndIncrement());
            this.channel.offer(request);
            System.out.println(Thread.currentThread().getName()+" put on the channel:"+request
                    .toString());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
